home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- /*
- * psfold -
- * Fold pages in half for book generation
- *
- * To compile:
- * cc psfold.c -o psfold
- *
- * Paul Haeberli - 1993
- */
- #include "stdio.h"
- #include "math.h"
- #include "ctype.h"
-
- #define INBUFSIZE 4096
- #define MAXPAGES 1000
-
- char buf[INBUFSIZE];
- int secstart[MAXPAGES];
- int secend[MAXPAGES];
- int trailstart;
- int trailend;
- float pdx, pdy, pscale;
- float pwidth, pheight;
-
- indexpages(inf)
- FILE *inf;
- {
- int i, n, before, after;
-
- n = 0;
- trailstart = 0;
- trailend = 0;
- while(fgets(buf,INBUFSIZE,inf)) {
- if(strncmp(buf,"%%Page:",7) == 0) {
- after = ftell(inf);
- if(n>0)
- secend[n-1] = before;
- secstart[n] = after;
- n++;
- } else if(strncmp(buf,"%%Trailer",9) == 0) {
- after = ftell(inf);
- if(n>0)
- secend[n-1] = before;
- trailstart = after;
- trailend = sizeoffile(inf);
- return n;
- }
- before = ftell(inf);
- }
- secend[n-1] = ftell(inf);
- return n;
- }
-
- putpage(inf,pageno,npages)
- FILE *inf;
- int pageno, npages;
- {
- int nbytes;
-
- if(pageno<npages && pageno>=0) {
- nbytes = secend[pageno]-secstart[pageno];
- putsection(inf,secstart[pageno],nbytes);
- }
- }
-
- putsection(inf,start,nbytes)
- FILE *inf;
- int start, nbytes;
- {
- int nr;
-
- fseek(inf,start,0);
- while(nbytes>0) {
- if(!fgets(buf,INBUFSIZE,inf)) {
- fprintf(stderr,"psbook: bad read in putsection\n");
- exit(1);
- }
- nr = strlen(buf);
- nbytes -= nr;
- if(buf[0] != '%')
- fputs(buf,stdout);
- }
- if(nbytes != 0) {
- fprintf(stderr,"psbook: strange read in putsection\n");
- exit(1);
- }
- }
-
- #define VERT (0)
- #define HORIZ (1)
-
- main(argc,argv)
- int argc;
- char **argv;
- {
- FILE *inf;
- int npages, folddir, i;
-
- if(argc<2) {
- fprintf(stderr,"\nusage: psfold in.ps [-hfold] [-vfold] [-s pagedx pagedy] > out.ps\n");
- exit(1);
- }
- folddir = VERT;
- pwidth = 8.5;
- pheight = 11.0;
- for(i=2; i<argc; i++) {
- if(argv[i][0] == '-') {
- switch(argv[i][1]) {
- case 'h':
- folddir = HORIZ;
- break;
- case 'v':
- folddir = VERT;
- break;
- case 's':
- i++;
- pwidth = atof(argv[i]);
- i++;
- pheight = atof(argv[i]);
- break;
- }
- }
- }
- if(folddir == VERT)
- fprintf(stderr,"psfold: output page size is -s %f %f\n",pwidth/2.0,pheight);
- else
- fprintf(stderr,"psfold: output page size is -s %f %f\n",pheight/2.0,pwidth);
- pwidth *= 72.0;
- pheight *= 72.0;
- printf("%%!PS-Adobe-2.0\n");
- printf("%%%%EndComments\n");
- inf = fopen(argv[1],"r");
- if(!inf) {
- fprintf(stderr,"psbook: can't open %s\n",argv[1]);
- exit(1);
- }
- npages = indexpages(inf);
- fprintf(stderr,"Document pages %d\n",npages);
- if(npages == 0) {
- fprintf(stderr,"psfold: Document does not have page info\n");
- exit(1);
- }
- putpages(inf,npages,folddir);
- fclose(inf);
- exit(0);
- }
-
- putpages(inf,npages,folddir)
- FILE *inf;
- int npages, folddir;
- {
- int pageno;
-
- printf("%%%%Page: page%d %d\n",1,1);
- printf("showpage\n");
- if(folddir == VERT) {
- for(pageno=0; pageno<npages; pageno++) {
- printf("%%%%Page: page%d %d\n",pageno+2,pageno+2);
- dosave(inf);
- printf("%f %f scale\n",1.0,1.0);
- printf("%f %f translate\n",0.0,0.0);
- putsection(inf,0,secstart[0]);
- putpage(inf,pageno,npages);
- dorestore(inf);
- printf("%%%%Page: page%d %d\n",pageno+2,pageno+2);
- dosave(inf);
- printf("%f %f scale\n",1.0,1.0);
- printf("%f %f translate\n",-pwidth/2.0,0.0);
- putsection(inf,0,secstart[0]);
- putpage(inf,pageno,npages);
- dorestore(inf);
- }
- } else {
- for(pageno=0; pageno<npages; pageno++) {
- printf("%%%%Page: page%d %d\n",pageno+2,pageno+2);
- dosave(inf);
- printf("%f %f scale\n",1.0,1.0);
- printf("%f %f translate\n",0.0,0.0);
- printf("%f %f translate\n",pwidth,0.0);
- printf("90 rotate\n");
- putsection(inf,0,secstart[0]);
- putpage(inf,pageno,npages);
- dorestore(inf);
- printf("%%%%Page: page%d %d\n",pageno+2,pageno+2);
- dosave(inf);
- printf("%f %f scale\n",1.0,1.0);
- printf("%f %f translate\n",-pheight/2.0,0.0);
- printf("%f %f translate\n",pwidth,0.0);
- printf("90 rotate\n");
- putsection(inf,0,secstart[0]);
- putpage(inf,pageno,npages);
- dorestore(inf);
- }
- }
- printf("%%%%EOF\n");
- }
-
- dosave(inf)
- FILE *inf;
- {
- printf("gsave\n");
- }
-
- dorestore(inf)
- FILE *inf;
- {
- if(trailstart)
- putsection(inf,trailstart,trailend-trailstart);
- printf("grestore\n");
- }
-